/*******************************************************************************
 * Copyright (c) 2010 L.Carbonnaux.
 *******************************************************************************/
package org.lcx.taskvision.core.util;

import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.eclipse.core.runtime.Assert;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.lcx.taskvision.core.ITaskVisionClient;
import org.lcx.taskvision.core.ITaskVisionConstants;
import org.lcx.taskvision.core.attribute.TaskVisionAttribute;

import com.google.gdata.data.spreadsheet.ListEntry;

/**
 * @author Laurent Carbonnaux
 */
public class TaskVisionUtil {

	private static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
	
	public static Date parseDate(String dateString) {
		if (dateString == null || dateString.length() == 0) {
			return null;
		}
		try {
			return sdf.parse(dateString);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static String toDate(Date date) {
		if (date == null) {
			return null;
		}
		return sdf.format(date);
	}

	public static void setFibonacciOptions(TaskAttribute att){
		att.putOption("---", "---");
		att.putOption("0", "0");
		att.putOption("1", "1");
		att.putOption("2", "2");
		att.putOption("3", "3");
		att.putOption("5", "5");
		att.putOption("8", "8");
		att.putOption("13", "13");
		att.putOption("20", "20");
		att.putOption("40", "40");
		att.putOption("100", "100");
	}

	public static String stripSpaces(String url) {
		Assert.isNotNull(url);
		StringBuilder sb = new StringBuilder(url.trim());
		while (sb.length() > 0 && sb.charAt(sb.length() - 1) == ' ') {
			sb.deleteCharAt(sb.length() - 1);
		}
		return sb.toString();
	}

	public static String getTaskIdFromEntry(ListEntry task) {
		return task.getCustomElements().getValue(TaskVisionAttribute.TASKID.getSvKey());
	}

	public static void setTaskId(ListEntry task, String taskId) {
		task.getCustomElements().setValueLocal(TaskVisionAttribute.TASKID.getSvKey(), taskId);
	}
	
	public static URL getRepositoryListUrl(TaskRepository repository) {
		URL url = null;
		try {
			url = new URL(repository.getRepositoryUrl());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return url;
	}
	
	public static URL getCellUrlForPrefix(TaskRepository repository, String prefix) {
		String surl = null;
		if(prefix.startsWith(ITaskVisionConstants.TASK_TYPE.TASK.getPrefix())) {
			// including prefix + "."
			String wsName = prefix.substring(ITaskVisionConstants.TASK_TYPE.TASK.getPrefix().length()+1);
			String attName = ITaskVisionClient.PROPERTY_TASK_CELL_FEED_URL+wsName;
			surl = repository.getProperty(attName);
		}
		URL url = null;
		try {
			url = new URL(surl);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return url;
	}
	
	public static String getSprintName(String prefix) {
		if(prefix.startsWith(ITaskVisionConstants.TASK_TYPE.TASK.getPrefix())) {
			// including prefix + "."
			return prefix.substring(ITaskVisionConstants.TASK_TYPE.TASK.getPrefix().length()+1);
		}
		return null;
	}

	public static String getSprintTaskId(String prefix) {
		String name = TaskVisionUtil.getSprintName(prefix);
		if(name.startsWith(ITaskVisionConstants.WORKSHEET_SPRINT_PREF)) {
			return ITaskVisionConstants.TASK_TYPE.SPRINT.getPrefix() + "#" + name.substring(ITaskVisionConstants.WORKSHEET_SPRINT_PREF.length());
		}
		return null;
	}
}
